/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.hr.recruitment;

import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dto.hr.recruitment.RecruitmentManagementDTO;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ObjResult;
import com.plian.system.service.hr.recruitment.IRecruitmentManagementService;
import com.plian.system.service.sys.code.IFormCodeService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.wrapper.hr.recruitment.RecruitmentManagementWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * 招聘管理 控制器
 *
 * @author gangan
 * @since 2019-11-15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/recruitmentmanagement")
public class RecruitmentManagementController extends BaseController {

	private IRecruitmentManagementService recruitmentManagementService;

	private FormWorkflowService formWorkflowService;

	private IFormCodeService formCodeService;

	private RecruitmentManagementWrapper recruitmentManagementWrapper;

//    /** TODO DEL
//    * 详情
//    */
//	@PostMapping("/detail")
//	public R<RecruitmentManagementVO> detail(@Valid @RequestBody RecruitmentManagement recruitmentManagement) {
//		RecruitmentManagement detail = recruitmentManagementService.getOne(Condition.getQueryWrapper(recruitmentManagement));
//		RecruitmentManagementVO recruitmentManagementVO = recruitmentManagementWrapper.entityToVO(detail);
//		return recruitmentManagementService.getDetailObj(recruitmentManagementVO);
//	}
//
//	/**
//	* 分页 招聘管理
//	*/
//	@PostMapping("/list")
//	public ListResult<RecruitmentManagementVO> list(@Valid @RequestBody Query query) {
//		if(query.getAscs()==null&&query.getDescs()==null){
//			query.setDescs("create_time");
//		}
//		List<SearchEntity> entityList = query.getSearch();
//		QueryWrapper<RecruitmentManagement> queryWrapper = SearchParamEntiy.getQueryWrapper(
//				Condition.getQueryWrapper(new RecruitmentManagement()), entityList
//		);
//		TokenSearch<RecruitmentManagement> tokenSearch = new TokenSearch<>();
//		tokenSearch.addTokenQuery(queryWrapper, query);
//		IPage<RecruitmentManagement> pages = recruitmentManagementService.page(Condition.getPage(query), queryWrapper);
//		List<RecruitmentManagementVO> recruitmentManagementVOS = recruitmentManagementWrapper.entityToVO(pages.getRecords());
//		ListResult<RecruitmentManagementVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//		listResult.setList(recruitmentManagementVOS);
//		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//		return listResult;
//	}
//
//	/**
//	* 新增或修改 招聘管理
//	*/
//	@PostMapping("/submit")
//	@Resubmit(delaySeconds = 0)
//	public R submit(@Valid @RequestBody RecruitmentManagementVO recruitmentManagementVO) {
//		RecruitmentManagement recruitmentManagement = new RecruitmentManagement();
//		BeanUtils.copyProperties(recruitmentManagementVO, recruitmentManagement);
//		if(recruitmentManagement.getId()==null){
//			recruitmentManagement.setVersion("1");
//			recruitmentManagement.setCode(formCodeService.getCode(FormTypeConstant.HR_RECRUITMENT_FORM));
//			recruitmentManagement.setFormType(String.valueOf(FormTypeConstant.HR_RECRUITMENT_FORM));
//		}
//		Boolean result = recruitmentManagementService.saveOrUpdate(recruitmentManagement);
//		if(result) {
//			recruitmentManagementVO.setId(recruitmentManagement.getId());
//			recruitmentManagementService.saveChild(recruitmentManagementVO);
//		}
//
//		HashMap<String, Object> processVariable = new HashMap<>();
//		formWorkflowService.startProcessAndSimulate(String.valueOf(recruitmentManagement.getId()),recruitmentManagement.getFormType(),processVariable);
//
//		return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
//	}
//
//	/**
//	* 删除 招聘管理
//	*/
//	@PostMapping("/remove")
//	public R remove(@RequestBody PageData pd) {
//		List<String> ids = (List<String>)pd.get("ids");
//		return recruitmentManagementService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
//				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
//	}
//
//    @PostMapping("/report")
//    public ObjResult reportTable(@RequestBody RecruitmentManagementDTO recruitmentManagementDTO) {
//        ReportReturnVO reportReturnVO = recruitmentManagementService.report(recruitmentManagementDTO);
//        ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
//        if (Optional.ofNullable(reportReturnVO).isPresent()) {
//            PageData pd = new PageData();
//            pd.put("grids", reportReturnVO.getGrids());
//            objResult.setPageData(pd);
//        }
//        return objResult;
//
//	}

	@PostMapping("/attributeReport")
	public ObjResult attributeReport(@RequestBody RecruitmentManagementDTO recruitmentManagementDTO) {
		ReportReturnVO reportReturnVO = recruitmentManagementService.attributeReport(recruitmentManagementDTO);
		ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
		if (Optional.ofNullable(reportReturnVO).isPresent()){
			PageData pd = new PageData();
			pd.put("grids", reportReturnVO.getGrids());
			objResult.setPageData(pd);
		}
		return objResult;

	}
}
